package cn.com.jrr.core.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.ResponseMessage;
import cn.com.jrr.core.model.TestModel;
import cn.com.jrr.core.service.UserService;
@SuppressWarnings("all")
@Controller
@RequestMapping("/demo")
public class Demo {
    @Autowired
    private UserService UserService;
    @Autowired
    private TestModel testModel;
    @Autowired
    private DataTableResponse<TestModel> dt;
    @Autowired
    private ResponseMessage responseMessage;

    @RequestMapping("/demo_1")
    public String demo_1(Map<String,Object> map){
        return "/demo/demo_1";
    }
    /**
     * 查询数据
     * */
    @ResponseBody
    @RequestMapping("/list")
    public DataTableResponse<TestModel> demoList(DataTableRequest dataTableRequest){
        dt=UserService.getAllUser(dataTableRequest);
        return dt;
    }
    /**
     * 添加数据
     * */
    @ResponseBody
    @RequestMapping("/add")
    public ResponseMessage demoAdd(TestModel testModel){
        System.out.println("success");
        int res=UserService.add(testModel);
        if(res<=0){
            responseMessage.setStatus("fail");
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
    /**
     * 获取一条数据
     * */
    @ResponseBody
    @RequestMapping("/get")
    public TestModel demoGet(@RequestParam Integer id){
        testModel=UserService.get(id);
        return testModel;
    }
    /**
     * 修改数据
     * */
    @ResponseBody
    @RequestMapping("/update")
    public ResponseMessage demoUpdate(TestModel testModel){
        int res=UserService.update(testModel);
        if(res<=0){
            responseMessage.setStatus("fail");
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
    /**
     * 删除数据
     * */
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseMessage demoDelete(@RequestParam String idString){
        List<Integer> idList=ToolUtils.splitIdString(idString);
        for(Integer id:idList){
            UserService.delete(id);
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
    /**
     * 文件上传
     * */
    @RequestMapping("/upload")
    public String demoUpload(HttpServletRequest request){
        ToolUtils.uploadUtil(request, "");
        return "";
    }
    /**
     * 文件下载
     * */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(){
        return ToolUtils.downloadUtil("","");
    }


}
